package utils

import (
	"math/rand"
	"os"
	"path"
	"time"
)

func RandomCreateBytes(n int, alphabets ...byte) []byte {
	var alphaNum = []byte(`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`)

	if len(alphabets) == 0 {
		alphabets = alphaNum
	}
	var bytes = make([]byte, n)
	var randBy bool
	if num, err := rand.Read(bytes); num != n || err != nil {
		rand.Seed(time.Now().UnixNano())
		randBy = true
	}
	for i, b := range bytes {
		if randBy {
			bytes[i] = alphabets[rand.Intn(len(alphabets))]
		} else {
			bytes[i] = alphabets[b%byte(len(alphabets))]
		}
	}
	return bytes
}
func CreateFile(absolutelyPath string) (*os.File, error) {
	/*if FileExists(absolutelyPath) { //已经存在
		return nil, errors.New("file is exits")
	} else {*/

	errDir := os.MkdirAll(path.Dir(absolutelyPath), 0777)
	if errDir != nil {
		return nil, errDir
	} else {
		if file, errFile := os.Create(absolutelyPath); errFile == nil {
			return file, nil
		} else {
			return nil, errFile
		}
	}
	//}
}
